package com.myblog.manage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.myblog.manage.domain.Category;
import com.myblog.manage.domain.Tag;
import com.myblog.manage.domain.dto.ArticleDTO;
import com.myblog.manage.domain.vo.ArticleVo;
import com.myblog.manage.wrapper.ArticleWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.myblog.common.annotation.Log;
import com.myblog.common.core.controller.BaseController;
import com.myblog.common.core.domain.AjaxResult;
import com.myblog.common.enums.BusinessType;
import com.myblog.manage.domain.Article;
import com.myblog.manage.service.IArticleService;
import com.myblog.common.utils.poi.ExcelUtil;
import com.myblog.common.core.page.TableDataInfo;

/**
 * 文章管理Controller
 * 
 * @author mingo_sheep
 * @date 2025-05-05
 */
@RestController
@RequestMapping("/manage/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService articleService;

    // 获取所有分类
    @GetMapping("/categories")
    public List<Category> listAllCategories() {
        return articleService.listAllCategories();
    }

    // 获取所有标签
    @GetMapping("/tags")
    public List<Tag> listAllTags() {
        return articleService.listAllTags();
    }

    /**
     * 查询文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            Article article,
            @RequestParam(name = "categoryId", required = false) Long categoryId,
            @RequestParam(name = "tagId", required = false) Long tagId)
    {
        // 将参数封装到 map 中
        Map<String, Object> params = new HashMap<>();
        params.put("title", article.getTitle());
        params.put("categoryId", categoryId);
        params.put("tagId", tagId);

        startPage();
        List<ArticleVo> list = articleService.selectArticleVoList(params);
        return getDataTable(list);
    }

    /**
     * 导出文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:article:export')")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article)
    {
        List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        util.exportExcel(response, list, "文章管理数据");
    }

    /**
     * 获取文章管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(articleService.selectArticleById(id));
    }

    /**
     * 新增文章管理
     */
    @PreAuthorize("@ss.hasPermi('manage:article:add')")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArticleDTO dto)
    {

        return toAjax(articleService.insertArticle(dto.getArticle(), dto.getCategoryId(), dto.getTagIds()));
    }

    /**
     * 修改文章管理
     */
    @PreAuthorize("@ss.hasPermi('manage:article:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArticleWrapper wrapper)
    {
        Article article = wrapper.getArticle();
        System.out.println("Received article: " + article);
        if (article.getId() == null) {
            return AjaxResult.error("文章ID不能为空");
        }
        return toAjax(articleService.updateArticle(article));
    }

    /**
     * 删除文章管理
     */
    @PreAuthorize("@ss.hasPermi('manage:article:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(articleService.deleteArticleByIds(ids));
    }
}
